package com.github.aloxc.plugin.restplus.listener;

import com.github.aloxc.plugin.restplus.entity.RestConfig;
import com.github.aloxc.plugin.restplus.entity.UserCase;
import com.github.aloxc.plugin.restplus.navigator.RestServiceDetail;
import com.github.aloxc.plugin.restplus.navigator.RestServicesNavigator;
import com.github.aloxc.plugin.restplus.utils.JsonUtils;
import com.github.aloxc.plugin.restplus.utils.StringUtils;

import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Map;

public class UserCaseTreeListener extends MouseAdapter implements TreeSelectionListener {
    private RestServiceDetail restServiceDetail;

    public UserCaseTreeListener(RestServiceDetail restServiceDetail){

        this.restServiceDetail = restServiceDetail;
    }
    @Override
    public void valueChanged(TreeSelectionEvent e) {
        restServiceDetail.removeResponseTab();
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) restServiceDetail.getUserCaseTree().getLastSelectedPathComponent();
        if (node == null) {
            return;
        }
        int selectIndex = node.getParent().getIndex(node);
        if (null != node) {
            Object userObject = node.getUserObject();
            if (null != userObject) {
                if (userObject instanceof String) {
                    String name = userObject.toString();
                    RestConfig config = restServiceDetail.getProject().getComponent(RestServicesNavigator.class).getConfig();
                    Map<String, UserCase> userCaseMap = config.getUserCaseMap().get((restServiceDetail.getParentName() != null ? restServiceDetail.getParentName() + ":" : "") + restServiceDetail.getUrl());
                    if(userCaseMap == null){
                        return;
                    }
                    UserCase userCase = userCaseMap.get(UserCase.getUserCaseKey(name,restServiceDetail.getMethod()));
                    if (userCase.getHeaderMap() != null) {
                        restServiceDetail.getHeaderTextArea().setText(JsonUtils.toJson(userCase.getHeaderMap(), true));
                        restServiceDetail.getHeaderTextArea().format();
                    }
                    if (userCase.getParamMap() != null && restServiceDetail.getRequestParamsTextArea() != null) {
                        restServiceDetail.getRequestParamsTextArea().setText(JsonUtils.toJson(userCase.getParamMap(), true));
                        restServiceDetail.getRequestParamsTextArea().format();
                    }
                    if (userCase.getBodyMap() != null && restServiceDetail.getRequestBodyTextArea() != null) {
                        restServiceDetail.getRequestBodyTextArea().setText(JsonUtils.toJson(userCase.getBodyMap(), true));
                        restServiceDetail.getRequestBodyTextArea().format();
                    }
                    restServiceDetail.getRequestTestTextArea().setText("");
                    if (!StringUtils.isNullOrEmpty(userCase.getTest())) {
                        restServiceDetail.getRequestTestTextArea().setText(userCase.getTest());
                        restServiceDetail.getRequestTestTextArea().format();
                    }
                }
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);
        if (e.getSource() == restServiceDetail.getUserCaseTree() && e.getClickCount() == 2) {
            // 按照鼠标点击的坐标点获取路径
            TreePath selPath = restServiceDetail.getUserCaseTree().getPathForLocation(e.getX(), e.getY());
            if (selPath != null) {// 谨防空指针异常!双击空白处是会这样
                // 获取这个路径上的最后一个组件,也就是双击的地方
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) selPath.getLastPathComponent();
                restServiceDetail.getSendActionListener().send(null,null);
            }
        }
    }
}
